#!/usr/bin/env python
#coding=utf-8

import json
import time
from hashlib import md5
from decimal import Decimal

from flask import request, session, current_app

from codingabc.database import db
from codingabc.helpers import toint, log_debug, log_info, log_error
from app.models.role import Role
from app.models.user import User, UserAdmin
from app.models.menu import Menu


def get_user_name(uid=0):
    """ 获取用户昵称 """

    user_list = db.session.query(User.uid, User.nickname, User.mobile).all()

    USER_NAME = {}
    for user in user_list:
        # mobile =  user.mobile
        # nickname = user.nickname if user else nickname
        # nickname = user.username if not nickname else nickname
        # nickname = user.realname if not nickname else nickname
        # nickname = '%s****%s' % (mobile[:3], mobile[-4:]) if mobile and not nickname else nickname
        USER_NAME[user.uid] = user.nickname

    return USER_NAME


def role_name():
    """获取角色名称"""

    role_list = db.session.query(Role.role_id, Role.role_name).\
                    order_by(Role.role_id.asc()).all()

    ROLE_NAME = {}
    for role in role_list:
        ROLE_NAME[role.role_id] = role.role_name

    return ROLE_NAME


def get_role_list(role_id_list=[], uid=0):
    """角色列表"""
    role_list , user_role_id_list = [], []

    if uid > 0:
        user_role_id_list = db.session.query(UserAdmin.role_id).\
                                filter(UserAdmin.role_id > 1).\
                                filter(UserAdmin.uid == uid).all()
        user_role_id_list = map(lambda r_id: toint(r_id), user_role_id_list)

    q = db.session.query(Role.role_id, Role.role_name).\
        filter(Role.role_id > 1)

    if len(user_role_id_list) > 0:
        role_id_list += user_role_id_list
        role_id_list = list(set(role_id_list))  # 去除列表重复元素
        q = q.filter(Role.role_id.in_(user_role_id_list))

    if len(role_id_list) > 0:
        role_list_temp = q.filter(Role.role_id.in_(role_id_list)).\
                            order_by(Role.role_id.asc()).all()
    else:
        role_list_temp = q.order_by(Role.role_id.asc()).all()

    # 如果role_id=1,append超级管理员到role_list
    if 1 in role_id_list:
        role = {'name':u'超级管理员', 'value':1}
        role_list.append(role)

    for role in role_list_temp:
        role = {'name':role.role_name, 'value':role.role_id}
        role_list.append(role)

    return role_list


def user_role_id_list(uid=0):
    """获取用户角色id列表"""

    q = db.session.query(UserAdmin.role_id)

    if uid > 0:
        q = q.filter(UserAdmin.uid == uid)
    ua_list = q.all()
    role_id_list = map(lambda role:role.role_id, ua_list)

    return role_id_list


def get_main_permission_name():
    """获取主权限的名称"""

    main_permission_list =  db.session.query(Menu.menu_id, Menu.endpoint_name).\
                                filter(Menu.parent_id > 0).\
                                order_by(Menu.menu_id.desc()).all()

    MAIN_PERMISSION_NAME = {}

    for mp in main_permission_list:
        MAIN_PERMISSION_NAME[mp.menu_id] = mp.endpoint_name

    return MAIN_PERMISSION_NAME


def get_menu_name():
    """获取菜单名称"""

    menu_list =  db.session.query(Menu.menu_id, Menu.endpoint_name).\
                                order_by(Menu.menu_id.desc()).all()

    MENU_NAME = {}

    for m in menu_list:
        MENU_NAME[m.menu_id] = m.endpoint_name

    return MENU_NAME

